package com.sto.stoapp.activity.scanner.delete;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.sto.stoapp.activity.R;
import com.sto.stoapp.dataprocess.ProcessScanCode;
import com.sto.stoapp.db.StoDataBaseOperation;
import com.sto.stoapp.manager.SoundManager;
import com.sto.stoapp.scan.ScanRuleManager;
import com.sto.stoapp.status.EnumTableNames;
import com.sto.stoapp.status.EnumScanCode;
import com.sto.stoapp.util.Helper;
import com.sto.stoapp.widget.StoHhmEditText;
import com.sto.stoapp.widget.StoHhmEditText.EditClickListener;
import com.sto.stoapp.widget.StoListenerActivity;

@SuppressLint("HandlerLeak")
public class StoDeleteDetailActivity extends StoListenerActivity {

	private StoHhmEditText code;

	// private String tableName;

	private int tableItem = -1, count = 0;
	private List<EditText> edit = new ArrayList<EditText>();
	private TextView alreadyDelete, totalDelete, delNoticeTxt;

	/**
	 * 装袋发件删除
	 */
	private void delBaggingAndSsendpiece(String code) {
		// 袋号不能删除
		if (code.substring(0, 3).equals("900")) {
			return;
		}

		delBaggingAndSsendpiece(EnumTableNames.values()[tableItem], code);

	}

	/**
	 * 装袋发件删除
	 */
	private void delBaggingAndSsendpiece(final EnumTableNames TABLE_STATUS,
			String code) {
		// 装袋&发件特殊处理
		if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_BAGGINGADNSENDPIECE) {
			// 统计袋号的个数
			int count = StoDataBaseOperation.getInstance(this)
					.StatisticsBagNumberCount(TABLE_STATUS, code);
			if (count == 1) {
				String bagNumber = StoDataBaseOperation.getInstance(this)
						.getBagNumber(TABLE_STATUS, code);
				StoDataBaseOperation
						.getInstance(this)
						.deleteScanData(
								EnumTableNames.STATUS_TABLE_SCANNING_BAGGING_OPERATE
										.ordinal(), bagNumber);
				this.deleteCodeNumber();
			}

		}
		// 中转-装袋&发件特殊处理
		else if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_CENTER_BAGGINGADNSENDPIECE) {
			// 统计袋号的个数
			int bag_count = StoDataBaseOperation.getInstance(this)
					.StatisticsBagNumberCount(TABLE_STATUS, code);
			if (bag_count == 1) {
				String bagNumber = StoDataBaseOperation.getInstance(this)
						.getBagNumber(TABLE_STATUS, code);
				StoDataBaseOperation
						.getInstance(this)
						.deleteScanData(
								EnumTableNames.STATUS_CENTER_TABLE_BAGGING_OPERATE
										.ordinal(), bagNumber);
				this.deleteCodeNumber();
			}

		}
		// 航空-扫描-装包&发件特殊处理
		else if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_AVIATION_SCAN_PACKET_SEND) {
			// 统计包号的个数
			int bag_count = StoDataBaseOperation.getInstance(this)
					.StatisticsBagNumberCount(TABLE_STATUS, code);
			if (bag_count == 1) {
				String bagNumber = StoDataBaseOperation.getInstance(this)
						.getBagNumber(TABLE_STATUS, code);
				// 删除包操作记录
				StoDataBaseOperation
						.getInstance(this)
						.deleteScanData(
								EnumTableNames.STATUS_TABLE_AVIATION_PACKAGE_OPERATE
										.ordinal(), bagNumber);
				this.deleteCodeNumber();

			}

		} else {
		}

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sto_error_detail);
		int nameItem = this.getIntent().getIntExtra("inquiryNameItem", -1);
		tableItem = this.getIntent().getIntExtra("inquiryTableItem", -1);
		Bundle extras = getIntent().getExtras();
		this.setTitleBottom(nameItem, extras.getString("busiType"));

		initWidget();
		initListener();
	}

	private void initWidget() {
		delNoticeTxt = (TextView) this.findViewById(R.id.delete_notice_text);
		alreadyDelete = (TextView) this.findViewById(R.id.already_delete_info);
		totalDelete = (TextView) this.findViewById(R.id.total_delete_info);
		code = (StoHhmEditText) this.findViewById(R.id.del_sanning_code);
		code.setSelectAllOnFocus(true);
		initPointLayout(false);
		edit.add(code);
		if (checkCarCode()) {
			delNoticeTxt.setText(R.string.code_car_delete);
		}
	}

	private boolean checkCarCode() {
		if (tableItem == EnumTableNames.STATUS_TABLE_AVIATION_SEND_CAR
				.ordinal()
				|| tableItem == EnumTableNames.STATUS_TABLE_AVIATION_RECEIVE_CAR
						.ordinal()
				|| tableItem == EnumTableNames.STATUS_TABLE_CENTER_SEND_CAR
						.ordinal()
				|| tableItem == EnumTableNames.STATUS_TABLE_CENTER_RECEIVE_CAR
						.ordinal()
				|| tableItem == EnumTableNames.STATUS_TABLE_SEND_CAR.ordinal()
				|| tableItem == EnumTableNames.STATUS_TABLE_RECEIVE_CAR
						.ordinal()) {
			return true;
		} else {
			return false;
		}
	}

	/**
	 * 
	 * modify: add code Listener
	 */
	private void initListener() {

		code.setEditDelete(new EditClickListener() {

			@Override
			public void onEditDelete() {
				StoDeleteDetailActivity.this.finish();
			}

			@Override
			public void onEditClickListener(View v) {
				onScanResults(code.getText().toString());
			}
		});

		code.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub
				if (checkCarCode()) {

					if (code.getText().toString().trim().length() == 9
							&& Helper.isNumeric(code.getText().toString()
									.trim())) {
						code.setText("STO" + code.getText().toString().trim());
						code.setSelection(12);
						// code.selectAll();
					}
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub

			}
		});
	}

	@Override
	protected void handlerScanData(String scanData) {

		if (scanData != null && !scanData.toString().equals("")) {
			String result = scanData.toString();
			EnumScanCode status = null;
			if (tableItem == EnumTableNames.STATUS_TABLE_LOADCAR_SENDPIECE
					.ordinal()
					|| tableItem == EnumTableNames.STATUS_TABLE_CENTER_LOADCAR_SENDPIECE
							.ordinal()
					|| tableItem == EnumTableNames.STATUS_TABLE_AVIATION_LOADCAR_SENDPIECE
							.ordinal()) {
				if (ScanRuleManager.getInstance(StoDeleteDetailActivity.this)
						.isCarSealCode(scanData)
						|| ScanRuleManager.getInstance(
								StoDeleteDetailActivity.this).isWaybillCode(
								scanData)) {
					status = EnumScanCode.CODE_WAY_BILL;
				} else {
					status = EnumScanCode.CODE_ILLEGAL;
				}
			} else {
				status = ProcessScanCode.getInstance(this).ScanCodeResolve(
						result, EnumTableNames.values()[tableItem]);
			}
			code.setText("");
			code.setText(result);
			code.setSelection(result.length());
			switch (status) {
			// 条码空
			case CODE_EMPTY:
				Helper.showSoundToast(StoDeleteDetailActivity.this,
						getString(R.string.code_empty), false);
				break;

			// 非法条码
			case CODE_ILLEGAL:
				Helper.showSoundToast(StoDeleteDetailActivity.this,
						getString(R.string.error_code), false);
				break;
			case CODE_SEAL_OK:

				// 成功条码
			case CODE_WAY_BILL:

				delBaggingAndSsendpiece(result);

				// 订单提取的
				if (tableItem == EnumTableNames.STATUS_TABLE_SALESMAN_ORDER_EXTRACT
						.ordinal()) {
					if (StoDataBaseOperation
							.getInstance(StoDeleteDetailActivity.this)
							.deleteScanData(
									EnumTableNames.STATUS_TABLE_SALESMAN_RECEIVE
											.ordinal(), result) > 0) {
						StoDeleteDetailActivity.this.deleteCodeNumber();
					}

				}

				delLoadCarSendPiece(EnumTableNames.values()[tableItem], result);

				int delCount = StoDataBaseOperation.getInstance(
						StoDeleteDetailActivity.this).deleteScanData(tableItem,
						result);
				if (delCount > 0) {
					SoundManager.getInstance(StoDeleteDetailActivity.this)
							.playSuccessSound();
					alreadyDelete.setText("");
					alreadyDelete.setText(result);
					count += delCount;
					totalDelete.setText("" + count);

					this.deleteCodeNumber(delCount);

				} else {
					Helper.showSoundToast(StoDeleteDetailActivity.this,
							getString(R.string.already_upload_notexit), false);
				}
				break;
			default:
				break;
			}
			code.setText("");
		}

	}

	/**
	 * 装车发件删除
	 * 
	 * @author wxf
	 * 
	 * @param currCode
	 */
	private void delLoadCarSendPiece(final EnumTableNames TABLE_STATUS,
			String currCode) {

		// 统计铅封号的个数
		int leadSealingcount = StoDataBaseOperation.getInstance(this)
				.StatisticsBagNumberCount(TABLE_STATUS, currCode);

		// 删除航空-扫描-装车发件记录
		if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_AVIATION_LOADCAR_SENDPIECE) {
			if (leadSealingcount == 1) {
				String leadSealingCode = StoDataBaseOperation.getInstance(this)
						.getBagNumber(TABLE_STATUS, currCode);
				// 删除铅封操作记录
				StoDataBaseOperation
						.getInstance(this)
						.deleteScanData(
								EnumTableNames.STATUS_TABLE_AVIATION_LEAD_SEALING_OPERATE
										.ordinal(), leadSealingCode);
				this.deleteCodeNumber();

			}
		}
		// 删除-中转-装车发件记录
		if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_CENTER_LOADCAR_SENDPIECE) {
			if (leadSealingcount == 1) {
				String leadSealingCode = StoDataBaseOperation.getInstance(this)
						.getBagNumber(TABLE_STATUS, currCode);
				// 删除铅封操作记录
				StoDataBaseOperation
						.getInstance(this)
						.deleteScanData(
								EnumTableNames.STATUS_TABLE_CENTER_LEAD_SEALING_OPERATE
										.ordinal(), leadSealingCode);
				this.deleteCodeNumber();

			}
		}
		// 删除-扫描员-装车发件记录
		if (TABLE_STATUS == EnumTableNames.STATUS_TABLE_LOADCAR_SENDPIECE) {

			if (leadSealingcount == 1) {
				String leadSealingCode = StoDataBaseOperation.getInstance(this)
						.getBagNumber(TABLE_STATUS, currCode);
				// 删除铅封操作记录
				StoDataBaseOperation
						.getInstance(this)
						.deleteScanData(
								EnumTableNames.STATUS_TABLE_SCANNING_LEAD_SEALING_OPERATE
										.ordinal(), leadSealingCode);
				this.deleteCodeNumber();

			}
		}
	}

}
